package com.imau.service.impl;

import com.imau.bean.Brand;
import com.imau.bean.Page;
import com.imau.common.SqlSessionFactoryUtil;
import com.imau.mapper.BrandMapper;
import com.imau.service.BrandService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    private BrandMapper mapper ;
    public boolean add(Brand brand) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        boolean flag=mapper.insertBrand(brand) > 0 ? true :false;

        session.close();

        return flag;
    }

    public boolean delete(Integer id) {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        boolean flag=mapper.deleteById(id) > 0 ? true :false;

        session.close();

        return flag;
    }

    public boolean edit(Brand brand) {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        boolean flag=mapper.updateBrand(brand) > 0 ? true :false;

        session.close();

        return flag;
    }

    public Brand getById(Integer id) {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        Brand brand = mapper.findById(id);

        session.close();

        return brand;
    }

    public List<Brand> queryAll() {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        session.close();

        return brands;
    }

    public Page<Brand> queryBypage(Long currentPage, Long size) {

        Page<Brand> brandPage = new Page<Brand>();

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        Long count = mapper.selectCount();
        //设置每页显示条数
        brandPage.setSize(size);
        //设置总条数
        brandPage.setTotal(count);
        //设置总页面数
        brandPage.setPages();
        //设置当前页码
        brandPage.setCurrentPage(currentPage);

        brandPage.setRows(mapper.selectByPage((currentPage-1)*size,size));

        session.close();

        return brandPage;
    }

    /*查询条件分页*/
    public Page<Brand> queryBypageAndCondition(Long currentPage, Long size,Brand brand) {
        Page<Brand> brandPage = new Page<Brand>();

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);

        Long count = mapper.selectCountAndCondition(brand);
        //设置每页显示条数
        brandPage.setSize(size);
        //设置总条数
        brandPage.setTotal(count);
        //设置总页面数
        brandPage.setPages();
        //设置当前页码
        brandPage.setCurrentPage(currentPage);

        brandPage.setRows(mapper.selectByPageAndCondition((currentPage-1)*size,size,brand));

        session.close();

        return brandPage;
    }

    public boolean deleteBatchByIds(Integer[] ids) {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        mapper = session.getMapper(BrandMapper.class);



        boolean flag=mapper.deleteBatchByIds(ids) > 0 ? true :false;

        session.close();

        return flag;

    }
}
